//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "OvernightIndexedSwapIndex.h"
using namespace Cephei::QL::Indexes;
#include <gen/QL/Indexes/OvernightIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Instruments/OvernightIndexedSwap.h>
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Instruments/VanillaSwap.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex) : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
    CPeriod^ _Ctenor;
    CCurrency^ _Ccurrency;
    COvernightIndex^ _CovernightIndex;
    try
    {
#ifdef HANDLE
        _phOvernightIndexedSwapIndex = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName); //d
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency _currency = static_cast<QuantLib::Currency> (_Ccurrency->GetReference ()); 
        _CovernightIndex = safe_cast<COvernightIndex^> (overnightIndex);
        _CovernightIndex->Lock();
        boost::shared_ptr<QuantLib::OvernightIndex>& _overnightIndex = static_cast<boost::shared_ptr<QuantLib::OvernightIndex>&> (_CovernightIndex->GetShared ()); 
        _ppOvernightIndexedSwapIndex = new boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex> (new QuantLib::OvernightIndexedSwapIndex ( _familyName,  _tenor,  _settlementDays,  _currency,  _overnightIndex ));
        SetSwapIndex (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppOvernightIndexedSwapIndex));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_CovernightIndex != nullptr) _CovernightIndex->Unlock();
    }
}
Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex>& childNative, Object^ owner) : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndexedSwapIndex = NULL;
#endif
	_ppOvernightIndexedSwapIndex = &childNative;
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppOvernightIndexedSwapIndex));
}
Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (QuantLib::OvernightIndexedSwapIndex& childNative, Object^ owner) : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndexedSwapIndex = NULL;
#endif
	_ppOvernightIndexedSwapIndex = new boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex> (&childNative);
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppOvernightIndexedSwapIndex));
    _OvernightIndexedSwapIndexOwner = owner;
    _SwapIndexOwner = owner;
}

Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (COvernightIndexedSwapIndex^ copy) : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndexedSwapIndex = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOvernightIndexedSwapIndex = new boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex> (copy->GetShared());
        _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppOvernightIndexedSwapIndex));
    }
}
Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (PLATFORM::Type^ t) : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndexedSwapIndex = NULL;
#endif
	if (!t->IsSubclassOf(COvernightIndexedSwapIndex::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (QuantLib::Handle<QuantLib::OvernightIndexedSwapIndex>& childNative, Object^ owner)  : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
	_phOvernightIndexedSwapIndex = &childNative;
	_ppOvernightIndexedSwapIndex = &static_cast<boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex>>(childNative.currentLink());
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppOvernightIndexedSwapIndex));
    _OvernightIndexedSwapIndexOwner = owner;
}
Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (QuantLib::Handle<QuantLib::OvernightIndexedSwapIndex> childNative)  : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
	_phOvernightIndexedSwapIndex = &childNative;
	_ppOvernightIndexedSwapIndex = &static_cast<boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex>>(childNative.currentLink());
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppOvernightIndexedSwapIndex));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::COvernightIndexedSwapIndex::COvernightIndexedSwapIndex (QuantLib::OvernightIndexedSwapIndex childNative)  : CSwapIndex(COvernightIndexedSwapIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndexedSwapIndex = NULL;
#endif
	_ppOvernightIndexedSwapIndex = new boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex> (new QuantLib::OvernightIndexedSwapIndex (childNative));
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppOvernightIndexedSwapIndex));
}
#endif

Cephei::QL::Indexes::COvernightIndexedSwapIndex::~COvernightIndexedSwapIndex ()
{
    if (_ppOvernightIndexedSwapIndex != NULL)
    {
	    delete _ppOvernightIndexedSwapIndex;
        _ppOvernightIndexedSwapIndex = NULL;
    }
}
Cephei::QL::Indexes::COvernightIndexedSwapIndex::!COvernightIndexedSwapIndex ()
{
    if (_ppOvernightIndexedSwapIndex != NULL)
    {
	    delete _ppOvernightIndexedSwapIndex;
    }
}
QuantLib::OvernightIndexedSwapIndex& Cephei::QL::Indexes::COvernightIndexedSwapIndex::GetReference ()
{
    if (_ppOvernightIndexedSwapIndex == NULL) throw REFNEW NativeNullException ();
	return **_ppOvernightIndexedSwapIndex;
}
boost::shared_ptr<QuantLib::OvernightIndexedSwapIndex>& Cephei::QL::Indexes::COvernightIndexedSwapIndex::GetShared ()
{
    if (_ppOvernightIndexedSwapIndex == NULL) throw REFNEW NativeNullException ();
	return *_ppOvernightIndexedSwapIndex;
}
QuantLib::OvernightIndexedSwapIndex* Cephei::QL::Indexes::COvernightIndexedSwapIndex::GetPointer ()
{
    if (_ppOvernightIndexedSwapIndex == NULL) throw REFNEW NativeNullException ();
	return &**_ppOvernightIndexedSwapIndex;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OvernightIndexedSwapIndex>& Cephei::QL::Indexes::COvernightIndexedSwapIndex::GetHandle ()
{
	if (_phOvernightIndexedSwapIndex == NULL)
	{
		_phOvernightIndexedSwapIndex = new Handle<QuantLib::OvernightIndexedSwapIndex> (*_ppOvernightIndexedSwapIndex);
	}
	return *_phOvernightIndexedSwapIndex;
}
#endif
bool Cephei::QL::Indexes::COvernightIndexedSwapIndex::HasNative () 
{
	return (_ppOvernightIndexedSwapIndex != NULL);
}

Cephei::QL::Indexes::IOvernightIndex^ Cephei::QL::Indexes::COvernightIndexedSwapIndex::OvernightIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::OvernightIndex> _rv = (boost::shared_ptr<QuantLib::OvernightIndex>)(*_ppOvernightIndexedSwapIndex)->overnightIndex ( );   
        Cephei::QL::Indexes::COvernightIndex^ _nrv = REFNEW Cephei::QL::Indexes::COvernightIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IOvernightIndexedSwap^ Cephei::QL::Indexes::COvernightIndexedSwapIndex::UnderlyingSwap (DateTime fixingDate)
{
    try
    {
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate); //a
    	boost::shared_ptr<QuantLib::OvernightIndexedSwap> _rv = (boost::shared_ptr<QuantLib::OvernightIndexedSwap>)(*_ppOvernightIndexedSwapIndex)->underlyingSwap ( _fixingDate );   
        Cephei::QL::Instruments::COvernightIndexedSwap^ _nrv = REFNEW Cephei::QL::Instruments::COvernightIndexedSwap (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::IOvernightIndexedSwapIndex^ Cephei::QL::Indexes::COvernightIndexedSwapIndex_Factory::Create (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex)
{
    return REFNEW COvernightIndexedSwapIndex ( familyName,  tenor,  settlementDays,  currency,  overnightIndex);
}
